package flc.ast.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.listener.OnPageChangeListener;

import java.util.List;

import flc.ast.AppConfig;
import flc.ast.R;
import flc.ast.activity.DetailActivity;
import flc.ast.activity.LatestActivity;
import flc.ast.activity.MapExplorationActivity;
import flc.ast.activity.StrategyDetailActivity;
import flc.ast.adapter.HomeIndicatorAdapter;
import flc.ast.adapter.LatestTabAdapter;
import flc.ast.adapter.MapTabAdapter;
import flc.ast.adapter.MyHomeBannerAdapter;
import flc.ast.databinding.FragmentHomeBinding;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;
import stark.common.bean.StkResBean;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HomeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HomeFragment extends BaseNoModelFragment<FragmentHomeBinding> {

    private MapTabAdapter mapTabAdapter;
    private LatestTabAdapter latestTabAdapter;
    private HomeIndicatorAdapter indicatorAdapter;

    @Override
    protected int onCreate() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);

        mDataBinding.ivMapMore.setOnClickListener(this);
        mDataBinding.ivLatestMore.setOnClickListener(this);

        mDataBinding.rvMapList.setLayoutManager(new GridLayoutManager(mContext, 2));
        mapTabAdapter = new MapTabAdapter();
        mDataBinding.rvMapList.setAdapter(mapTabAdapter);
        mapTabAdapter.setOnItemClickListener(this);

        mDataBinding.rvLatestList.setLayoutManager(new GridLayoutManager(mContext, 2));
        latestTabAdapter = new LatestTabAdapter();
        mDataBinding.rvLatestList.setAdapter(latestTabAdapter);
        latestTabAdapter.setOnItemClickListener(this);

        mDataBinding.rvIndicatorList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        indicatorAdapter = new HomeIndicatorAdapter();
        mDataBinding.rvIndicatorList.setAdapter(indicatorAdapter);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivMapMore://地图探索查看更多
                startActivity(MapExplorationActivity.class);
                break;
            case R.id.ivLatestMore://最新攻略查看更多
                startActivity(LatestActivity.class);
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (adapter == mapTabAdapter) {
            DetailActivity.stkResBean = mapTabAdapter.getItem(position);
            startActivity(DetailActivity.class);
        } else if (adapter == latestTabAdapter) {
            StrategyDetailActivity.stkResBean = latestTabAdapter.getItem(position);
            startActivity(StrategyDetailActivity.class);
        }
    }

    @Override
    protected void initData() {
        getBannerData();
        getMapData();
        getLatestData();
    }

    private void getBannerData() {
        StkResApi.getTagResourceList(this, AppConfig.BANNER_DATA, StkResApi.createParamMap(0, 6), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        indicatorAdapter.setList(result);
                        MyHomeBannerAdapter bannerAdapter = new MyHomeBannerAdapter(result);
                        mDataBinding.banner.setAdapter(bannerAdapter, true);//false不无限循环
                        mDataBinding.banner.isAutoLoop(true);//false不自动轮播
                        //点击监听
                        mDataBinding.banner.setOnBannerListener(new OnBannerListener() {
                            @Override
                            public void OnBannerClick(Object data, int position) {
                                DetailActivity.stkResBean = result.get(position);
                                startActivity(DetailActivity.class);
                            }
                        });
                    }
                    mDataBinding.banner.addOnPageChangeListener(new OnPageChangeListener() {
                        @Override
                        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                        }

                        @Override
                        public void onPageSelected(int position) {
                            indicatorAdapter.setSelPosition(position);
                            indicatorAdapter.notifyDataSetChanged();
                        }

                        @Override
                        public void onPageScrollStateChanged(int state) {

                        }
                    });
                }
            }
        });
    }

    private void getMapData() {
        StkResApi.getTagResourceList(this, AppConfig.MAP_DATA, StkResApi.createParamMap(0, 4), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        mapTabAdapter.setList(result);
                    }
                }
            }
        });
    }

    private void getLatestData() {
        StkResApi.getTagResourceList(this, AppConfig.LATEST_DATA, StkResApi.createParamMap(0, 4), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        latestTabAdapter.setList(result);
                    }
                }
            }
        });
    }

}